package org.hh.jga.util;

import java.util.Iterator;

import net.sf.jga.fn.Generator;

/**
 * Generator iterator
 * used to generate a range of values
 * 
 * @author Hong Hong
 *
 * @param Iterator type
 */
public class GeneratorIterator<T> implements Iterator<T> {
	protected Generator<T> m_func;
	protected int m_size;
	public GeneratorIterator(int size, Generator<T> func) {
		m_func = func;
		m_size = size;
	}
	public GeneratorIterator(int size, final Class<? extends T> c) {
		m_size = size;
		m_func = new Generator<T>() {
			private static final long serialVersionUID = -6559246826147818227L;
			@Override
			public T fn() {
				try {
					return c.newInstance();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	public boolean hasNext() {
		return m_size > 0;
	}

	public T next() {
		--m_size;
		return m_func.fn();
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}

}
